﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.RegistrationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.RegistrationHandler
{
    /// <summary>
    /// 获取医生信息
    /// </summary>
    public class GetPdoctorHandler:  IRequestHandler<GetPdoctorCommand, APIResult<List<PdoctorDto>>>
    {
        private readonly IBaseRepository<DoctorScheduling> baseRepository;
        private readonly IBaseRepository<Department> deparableRepository;
        private readonly IBaseRepository<Doctor> doctorRepository;
        private readonly IBaseRepository<TitleManagement> titlerep;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        /// <param name="deparableRepository"></param>
        /// <param name="doctorRepository"></param>

        public GetPdoctorHandler(IBaseRepository<DoctorScheduling> baseRepository, IBaseRepository<Department> deparableRepository, IBaseRepository<Doctor> doctorRepository,IBaseRepository<TitleManagement> titlerep)
        {
            this.baseRepository = baseRepository;
            this.deparableRepository = deparableRepository;
            this.doctorRepository = doctorRepository;
            this.titlerep = titlerep;
        }

        public Task<APIResult<List<PdoctorDto>>> Handle(GetPdoctorCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<PdoctorDto>> result = new APIResult<List<PdoctorDto>>();
            try
            {
                result.Code = APIEnum.Success;
                result.Msg = "查询成功";
                var linq = from d in baseRepository.GetAll()
                           join doc in doctorRepository.GetAll() on d.DoctorId equals doc.DoctorId
                           join de in deparableRepository.GetAll() on doc.DeparId equals de.DeparId
                           join tit in titlerep.GetAll() on doc.TitlemanagementId equals tit.TitleManagementId
                           select new PdoctorDto
                           {
                               DoctorId = doc.DoctorId,
                               DoctorName = doc.DoctorName,
                               TitlemanagementId = doc.TitlemanagementId,
                               JobName = tit.JobName,
                               Doctorlabel = doc.Doctorlabel,
                               Registrationfee = doc.Registrationfee,
                               Consultationfee = doc.Consultationfee,
                               Doctorintroduc = doc.Doctorintroduc,
                               DeparId = de.DeparId,
                               DeparName = de.DeparName,
                               MorningNumberSources = d.MorningNumberSources,
                               AfternoonNumberSources = d.AfternoonNumberSources,
                               MorningNumberPatients = d.MorningNumberPatients,
                               AfternoonNumberPatients = d.AfternoonNumberPatients,
                               Date = d.Date,
                               MorningAdmissions = d.MorningAdmissions,
                               AfternoonAdmissions = d.AfternoonAdmissions,
                               MorningIsNo = d.MorningIsNo,
                               AfternoonIsNo = d.AfternoonIsNo,
                               IsScheduling = d.IsScheduling,
                               SAfternoonNumberSources  = (d.AfternoonNumberSources-d.AfternoonNumberPatients),
                               SMorningNumberSources = (d.MorningNumberSources - d.MorningNumberPatients),
                               SumNumberSources = (d.AfternoonNumberSources - d.AfternoonNumberPatients) + (d.MorningNumberSources - d.MorningNumberPatients)
                           };
                if (request.DeparId != null)
                {
                    linq = linq.Where(x => x.DeparId == request.DeparId);

                }
                if (request.Date!= null)
                {
                    linq = linq.Where(x => x.Date.Date == request.Date.Value.Date);
                }
                result.Data = linq.ToList();
                return Task.FromResult(result);

            }
            catch (Exception)
            {

                throw;
            }
          


        }
    }
}
